package ext.acae2.change.utility;

import java.util.ArrayList;
import java.util.List;

import wt.change2.ChangeHelper2;
import wt.change2.WTChangeActivity2;
import wt.change2.WTChangeOrder2;
import wt.fc.QueryResult;
import wt.fc.WTObject;
import wt.session.SessionServerHelper;
import wt.util.WTException;

/**
 * @desription 
 *    change utility for all change api 
 * @author cnie
 * @date 2013-5-11 下午5:09:33
 */
public class ChangeUtility {
	
	
	/**
	 * 
	 * @description get changeActivity by ECN
	 * @author cnie
	 * @date 2013-5-11 下午5:17:28
	 * @version 1.0
	 * @param ecn
	 * @return
	 */
	public static List<WTChangeActivity2> getReleatedCA(WTChangeOrder2 ecn){
		List<WTChangeActivity2> caList = new ArrayList<WTChangeActivity2>();
		try{
			QueryResult qr = ChangeHelper2.service.getChangeActivities(ecn);
			while(qr.hasMoreElements()){
				WTChangeActivity2 ca = (WTChangeActivity2)qr.nextElement();
				caList.add(ca);
			}
		}catch(WTException e){
			e.printStackTrace();
		}
		return caList;
	}
	
	
	/**
	 * 
	 * @description 
	 *    get CA related before changeable object
	 * @author cnie
	 * @date 2013-5-11 下午5:20:20
	 * @version 1.0
	 * @param ca
	 * @return
	 */
	public static ArrayList<WTObject> getCAAffectedData(WTChangeActivity2 ca){
		ArrayList<WTObject> result = new ArrayList<WTObject>();
		boolean enforce = SessionServerHelper.manager.setAccessEnforced(false);
		try{
			QueryResult qr = ChangeHelper2.service.getChangeablesBefore(ca);
			while (qr.hasMoreElements()) {
				WTObject wtobject = (WTObject)qr.nextElement();
				result.add(wtobject);
			}
		}catch(WTException e){
			e.printStackTrace();
		}finally{
			SessionServerHelper.manager.setAccessEnforced(enforce);
		}
		return result;
	}
	
	
	public static ArrayList<WTObject> getCAResultingData(WTChangeActivity2 ca){
		ArrayList<WTObject> result = new ArrayList<WTObject>();
		boolean enforce = SessionServerHelper.manager.setAccessEnforced(false);
		try{
			QueryResult qr = ChangeHelper2.service.getChangeablesAfter(ca);
			while (qr.hasMoreElements()) {
				WTObject wtobject = (WTObject)qr.nextElement();
				result.add(wtobject);
			}
		}catch(WTException e){
			e.printStackTrace();
		}finally{
			SessionServerHelper.manager.setAccessEnforced(enforce);
		}
		return result;
	}
	
	
	
	

}
